package cn.edu.niit.server.web.rest;

import static cn.edu.niit.server.web.rest.TestUtil.sameInstant;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import cn.edu.niit.server.IntegrationTest;
import cn.edu.niit.server.domain.UserContentPraise;
import cn.edu.niit.server.repository.UserContentPraiseRepository;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
import javax.persistence.EntityManager;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;

/**
 * Integration tests for the {@link UserContentPraiseResource} REST controller.
 */
@IntegrationTest
@AutoConfigureMockMvc
@WithMockUser
class UserContentPraiseResourceIT {

    private static final ZonedDateTime DEFAULT_PRAISE_TIME = ZonedDateTime.ofInstant(Instant.ofEpochMilli(0L), ZoneOffset.UTC);
    private static final ZonedDateTime UPDATED_PRAISE_TIME = ZonedDateTime.now(ZoneId.systemDefault()).withNano(0);

    private static final String ENTITY_API_URL = "/api/user-content-praises";
    private static final String ENTITY_API_URL_ID = ENTITY_API_URL + "/{id}";

    private static Random random = new Random();
    private static AtomicLong count = new AtomicLong(random.nextInt() + (2 * Integer.MAX_VALUE));

    @Autowired
    private UserContentPraiseRepository userContentPraiseRepository;

    @Autowired
    private EntityManager em;

    @Autowired
    private MockMvc restUserContentPraiseMockMvc;

    private UserContentPraise userContentPraise;

    /**
     * Create an entity for this test.
     *
     * This is a static method, as tests for other entities might also need it,
     * if they test an entity which requires the current entity.
     */
    public static UserContentPraise createEntity(EntityManager em) {
        UserContentPraise userContentPraise = new UserContentPraise().praiseTime(DEFAULT_PRAISE_TIME);
        return userContentPraise;
    }

    /**
     * Create an updated entity for this test.
     *
     * This is a static method, as tests for other entities might also need it,
     * if they test an entity which requires the current entity.
     */
    public static UserContentPraise createUpdatedEntity(EntityManager em) {
        UserContentPraise userContentPraise = new UserContentPraise().praiseTime(UPDATED_PRAISE_TIME);
        return userContentPraise;
    }

    @BeforeEach
    public void initTest() {
        userContentPraise = createEntity(em);
    }

    @Test
    @Transactional
    void createUserContentPraise() throws Exception {
        int databaseSizeBeforeCreate = userContentPraiseRepository.findAll().size();
        // Create the UserContentPraise
        restUserContentPraiseMockMvc
            .perform(
                post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(userContentPraise))
            )
            .andExpect(status().isCreated());

        // Validate the UserContentPraise in the database
        List<UserContentPraise> userContentPraiseList = userContentPraiseRepository.findAll();
        assertThat(userContentPraiseList).hasSize(databaseSizeBeforeCreate + 1);
        UserContentPraise testUserContentPraise = userContentPraiseList.get(userContentPraiseList.size() - 1);
        assertThat(testUserContentPraise.getPraiseTime()).isEqualTo(DEFAULT_PRAISE_TIME);
    }

    @Test
    @Transactional
    void createUserContentPraiseWithExistingId() throws Exception {
        // Create the UserContentPraise with an existing ID
        userContentPraise.setId(1L);

        int databaseSizeBeforeCreate = userContentPraiseRepository.findAll().size();

        // An entity with an existing ID cannot be created, so this API call must fail
        restUserContentPraiseMockMvc
            .perform(
                post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(userContentPraise))
            )
            .andExpect(status().isBadRequest());

        // Validate the UserContentPraise in the database
        List<UserContentPraise> userContentPraiseList = userContentPraiseRepository.findAll();
        assertThat(userContentPraiseList).hasSize(databaseSizeBeforeCreate);
    }

    @Test
    @Transactional
    void getAllUserContentPraises() throws Exception {
        // Initialize the database
        userContentPraiseRepository.saveAndFlush(userContentPraise);

        // Get all the userContentPraiseList
        restUserContentPraiseMockMvc
            .perform(get(ENTITY_API_URL + "?sort=id,desc"))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.[*].id").value(hasItem(userContentPraise.getId().intValue())))
            .andExpect(jsonPath("$.[*].praiseTime").value(hasItem(sameInstant(DEFAULT_PRAISE_TIME))));
    }

    @Test
    @Transactional
    void getUserContentPraise() throws Exception {
        // Initialize the database
        userContentPraiseRepository.saveAndFlush(userContentPraise);

        // Get the userContentPraise
        restUserContentPraiseMockMvc
            .perform(get(ENTITY_API_URL_ID, userContentPraise.getId()))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.id").value(userContentPraise.getId().intValue()))
            .andExpect(jsonPath("$.praiseTime").value(sameInstant(DEFAULT_PRAISE_TIME)));
    }

    @Test
    @Transactional
    void getNonExistingUserContentPraise() throws Exception {
        // Get the userContentPraise
        restUserContentPraiseMockMvc.perform(get(ENTITY_API_URL_ID, Long.MAX_VALUE)).andExpect(status().isNotFound());
    }

    @Test
    @Transactional
    void putNewUserContentPraise() throws Exception {
        // Initialize the database
        userContentPraiseRepository.saveAndFlush(userContentPraise);

        int databaseSizeBeforeUpdate = userContentPraiseRepository.findAll().size();

        // Update the userContentPraise
        UserContentPraise updatedUserContentPraise = userContentPraiseRepository.findById(userContentPraise.getId()).get();
        // Disconnect from session so that the updates on updatedUserContentPraise are not directly saved in db
        em.detach(updatedUserContentPraise);
        updatedUserContentPraise.praiseTime(UPDATED_PRAISE_TIME);

        restUserContentPraiseMockMvc
            .perform(
                put(ENTITY_API_URL_ID, updatedUserContentPraise.getId())
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(TestUtil.convertObjectToJsonBytes(updatedUserContentPraise))
            )
            .andExpect(status().isOk());

        // Validate the UserContentPraise in the database
        List<UserContentPraise> userContentPraiseList = userContentPraiseRepository.findAll();
        assertThat(userContentPraiseList).hasSize(databaseSizeBeforeUpdate);
        UserContentPraise testUserContentPraise = userContentPraiseList.get(userContentPraiseList.size() - 1);
        assertThat(testUserContentPraise.getPraiseTime()).isEqualTo(UPDATED_PRAISE_TIME);
    }

    @Test
    @Transactional
    void putNonExistingUserContentPraise() throws Exception {
        int databaseSizeBeforeUpdate = userContentPraiseRepository.findAll().size();
        userContentPraise.setId(count.incrementAndGet());

        // If the entity doesn't have an ID, it will throw BadRequestAlertException
        restUserContentPraiseMockMvc
            .perform(
                put(ENTITY_API_URL_ID, userContentPraise.getId())
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(TestUtil.convertObjectToJsonBytes(userContentPraise))
            )
            .andExpect(status().isBadRequest());

        // Validate the UserContentPraise in the database
        List<UserContentPraise> userContentPraiseList = userContentPraiseRepository.findAll();
        assertThat(userContentPraiseList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void putWithIdMismatchUserContentPraise() throws Exception {
        int databaseSizeBeforeUpdate = userContentPraiseRepository.findAll().size();
        userContentPraise.setId(count.incrementAndGet());

        // If url ID doesn't match entity ID, it will throw BadRequestAlertException
        restUserContentPraiseMockMvc
            .perform(
                put(ENTITY_API_URL_ID, count.incrementAndGet())
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(TestUtil.convertObjectToJsonBytes(userContentPraise))
            )
            .andExpect(status().isBadRequest());

        // Validate the UserContentPraise in the database
        List<UserContentPraise> userContentPraiseList = userContentPraiseRepository.findAll();
        assertThat(userContentPraiseList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void putWithMissingIdPathParamUserContentPraise() throws Exception {
        int databaseSizeBeforeUpdate = userContentPraiseRepository.findAll().size();
        userContentPraise.setId(count.incrementAndGet());

        // If url ID doesn't match entity ID, it will throw BadRequestAlertException
        restUserContentPraiseMockMvc
            .perform(
                put(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(userContentPraise))
            )
            .andExpect(status().isMethodNotAllowed());

        // Validate the UserContentPraise in the database
        List<UserContentPraise> userContentPraiseList = userContentPraiseRepository.findAll();
        assertThat(userContentPraiseList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void partialUpdateUserContentPraiseWithPatch() throws Exception {
        // Initialize the database
        userContentPraiseRepository.saveAndFlush(userContentPraise);

        int databaseSizeBeforeUpdate = userContentPraiseRepository.findAll().size();

        // Update the userContentPraise using partial update
        UserContentPraise partialUpdatedUserContentPraise = new UserContentPraise();
        partialUpdatedUserContentPraise.setId(userContentPraise.getId());

        restUserContentPraiseMockMvc
            .perform(
                patch(ENTITY_API_URL_ID, partialUpdatedUserContentPraise.getId())
                    .contentType("application/merge-patch+json")
                    .content(TestUtil.convertObjectToJsonBytes(partialUpdatedUserContentPraise))
            )
            .andExpect(status().isOk());

        // Validate the UserContentPraise in the database
        List<UserContentPraise> userContentPraiseList = userContentPraiseRepository.findAll();
        assertThat(userContentPraiseList).hasSize(databaseSizeBeforeUpdate);
        UserContentPraise testUserContentPraise = userContentPraiseList.get(userContentPraiseList.size() - 1);
        assertThat(testUserContentPraise.getPraiseTime()).isEqualTo(DEFAULT_PRAISE_TIME);
    }

    @Test
    @Transactional
    void fullUpdateUserContentPraiseWithPatch() throws Exception {
        // Initialize the database
        userContentPraiseRepository.saveAndFlush(userContentPraise);

        int databaseSizeBeforeUpdate = userContentPraiseRepository.findAll().size();

        // Update the userContentPraise using partial update
        UserContentPraise partialUpdatedUserContentPraise = new UserContentPraise();
        partialUpdatedUserContentPraise.setId(userContentPraise.getId());

        partialUpdatedUserContentPraise.praiseTime(UPDATED_PRAISE_TIME);

        restUserContentPraiseMockMvc
            .perform(
                patch(ENTITY_API_URL_ID, partialUpdatedUserContentPraise.getId())
                    .contentType("application/merge-patch+json")
                    .content(TestUtil.convertObjectToJsonBytes(partialUpdatedUserContentPraise))
            )
            .andExpect(status().isOk());

        // Validate the UserContentPraise in the database
        List<UserContentPraise> userContentPraiseList = userContentPraiseRepository.findAll();
        assertThat(userContentPraiseList).hasSize(databaseSizeBeforeUpdate);
        UserContentPraise testUserContentPraise = userContentPraiseList.get(userContentPraiseList.size() - 1);
        assertThat(testUserContentPraise.getPraiseTime()).isEqualTo(UPDATED_PRAISE_TIME);
    }

    @Test
    @Transactional
    void patchNonExistingUserContentPraise() throws Exception {
        int databaseSizeBeforeUpdate = userContentPraiseRepository.findAll().size();
        userContentPraise.setId(count.incrementAndGet());

        // If the entity doesn't have an ID, it will throw BadRequestAlertException
        restUserContentPraiseMockMvc
            .perform(
                patch(ENTITY_API_URL_ID, userContentPraise.getId())
                    .contentType("application/merge-patch+json")
                    .content(TestUtil.convertObjectToJsonBytes(userContentPraise))
            )
            .andExpect(status().isBadRequest());

        // Validate the UserContentPraise in the database
        List<UserContentPraise> userContentPraiseList = userContentPraiseRepository.findAll();
        assertThat(userContentPraiseList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void patchWithIdMismatchUserContentPraise() throws Exception {
        int databaseSizeBeforeUpdate = userContentPraiseRepository.findAll().size();
        userContentPraise.setId(count.incrementAndGet());

        // If url ID doesn't match entity ID, it will throw BadRequestAlertException
        restUserContentPraiseMockMvc
            .perform(
                patch(ENTITY_API_URL_ID, count.incrementAndGet())
                    .contentType("application/merge-patch+json")
                    .content(TestUtil.convertObjectToJsonBytes(userContentPraise))
            )
            .andExpect(status().isBadRequest());

        // Validate the UserContentPraise in the database
        List<UserContentPraise> userContentPraiseList = userContentPraiseRepository.findAll();
        assertThat(userContentPraiseList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void patchWithMissingIdPathParamUserContentPraise() throws Exception {
        int databaseSizeBeforeUpdate = userContentPraiseRepository.findAll().size();
        userContentPraise.setId(count.incrementAndGet());

        // If url ID doesn't match entity ID, it will throw BadRequestAlertException
        restUserContentPraiseMockMvc
            .perform(
                patch(ENTITY_API_URL)
                    .contentType("application/merge-patch+json")
                    .content(TestUtil.convertObjectToJsonBytes(userContentPraise))
            )
            .andExpect(status().isMethodNotAllowed());

        // Validate the UserContentPraise in the database
        List<UserContentPraise> userContentPraiseList = userContentPraiseRepository.findAll();
        assertThat(userContentPraiseList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void deleteUserContentPraise() throws Exception {
        // Initialize the database
        userContentPraiseRepository.saveAndFlush(userContentPraise);

        int databaseSizeBeforeDelete = userContentPraiseRepository.findAll().size();

        // Delete the userContentPraise
        restUserContentPraiseMockMvc
            .perform(delete(ENTITY_API_URL_ID, userContentPraise.getId()).accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isNoContent());

        // Validate the database contains one less item
        List<UserContentPraise> userContentPraiseList = userContentPraiseRepository.findAll();
        assertThat(userContentPraiseList).hasSize(databaseSizeBeforeDelete - 1);
    }
}
